<template>
  <div id="contain">
    <div>
      <el-form
        :model="seachform"
        label-width="80px"
        class="seachform"
        style="width: 940px"
      >
        <el-form-item label="景点名称:" style="width: 220px">
          <el-input v-model="seachform.name"></el-input>
        </el-form-item>
        <el-form-item label="景点编号:" style="width: 200px">
          <el-input v-model="seachform.areaId"></el-input>
        </el-form-item>
        <el-form-item label="发布者:" style="width: 200px">
          <el-input v-model="seachform.realName"></el-input>
        </el-form-item>
        <el-form-item label="所属地区:" style="width: 200px">
          <el-select v-model="seachform.area" placeholder="请选择省份">
            <el-option label="全部" value=""></el-option>
            <el-option label="北京" value="北京"></el-option>
            <el-option label="天津" value="天津"></el-option>
            <el-option label="上海" value="上海"></el-option>
            <el-option label="重庆" value="重庆"></el-option>
            <el-option label="河北" value="河北"></el-option>
            <el-option label="河南" value="河南"></el-option>
            <el-option label="云南" value="云南"></el-option>
            <el-option label="黑龙江" value="黑龙江"></el-option>
            <el-option label="湖南" value="湖南"></el-option>
            <el-option label="安徽" value="安徽"></el-option>
            <el-option label="山东" value="山东"></el-option>
            <el-option label="江苏" value="江苏"></el-option>
            <el-option label="浙江" value="浙江"></el-option>
            <el-option label="江西" value="江西"></el-option>
            <el-option label="湖北" value="湖北"></el-option>
            <el-option label="甘肃" value="甘肃"></el-option>
            <el-option label="山西" value="山西"></el-option>
            <el-option label="陕西" value="陕西"></el-option>
            <el-option label="吉林" value="吉林"></el-option>
            <el-option label="福建" value="福建"></el-option>
            <el-option label="贵州" value="贵州"></el-option>
            <el-option label="广东" value="广东"></el-option>
            <el-option label="青海" value="青海"></el-option>
            <el-option label="四川" value="四川"></el-option>
            <el-option label="海南" value="海南"></el-option>
            <el-option label="台湾" value="台湾"></el-option>
            <el-option label="新疆" value="新疆"></el-option>
            <el-option label="广西" value="广西"></el-option>
            <el-option label="内蒙古" value="内蒙古"></el-option>
            <el-option label="宁夏" value="宁夏"></el-option>
            <el-option label="西藏" value="西藏"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="search" class="searchbtn"
          >搜索</el-button
        >
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableList"
        style="width: 945px"
        stripe
        :row-style="rowStyle"
        :cell-style="cellStyle"
        height="730px"
      >
        <el-table-column prop="name" label="景点名" align="center">
        </el-table-column>
        <el-table-column prop="address" label="地址" align="center">
        </el-table-column>
        <el-table-column prop="areaId" label="景点编号" align="center">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="mini" style="color: red" @click="clear(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[10, 15, 20]"
        :page-size="limit"
        layout="sizes, prev, pager, next"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog title="编辑景点信息" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="景点名称" style="width: 500px">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="所属省份">
          <el-select v-model="form.parentAreaName" placeholder="请选择省份">
            <el-option label="北京" value="北京"></el-option>
            <el-option label="天津" value="天津"></el-option>
            <el-option label="上海" value="上海"></el-option>
            <el-option label="重庆" value="重庆"></el-option>
            <el-option label="河北" value="河北"></el-option>
            <el-option label="河南" value="河南"></el-option>
            <el-option label="云南" value="云南"></el-option>
            <el-option label="黑龙江" value="黑龙江"></el-option>
            <el-option label="湖南" value="湖南"></el-option>
            <el-option label="安徽" value="安徽"></el-option>
            <el-option label="山东" value="山东"></el-option>
            <el-option label="江苏" value="江苏"></el-option>
            <el-option label="浙江" value="浙江"></el-option>
            <el-option label="江西" value="江西"></el-option>
            <el-option label="湖北" value="湖北"></el-option>
            <el-option label="甘肃" value="甘肃"></el-option>
            <el-option label="山西" value="山西"></el-option>
            <el-option label="陕西" value="陕西"></el-option>
            <el-option label="吉林" value="吉林"></el-option>
            <el-option label="福建" value="福建"></el-option>
            <el-option label="贵州" value="贵州"></el-option>
            <el-option label="广东" value="广东"></el-option>
            <el-option label="青海" value="青海"></el-option>
            <el-option label="四川" value="四川"></el-option>
            <el-option label="海南" value="海南"></el-option>
            <el-option label="台湾" value="台湾"></el-option>
            <el-option label="新疆" value="新疆"></el-option>
            <el-option label="广西" value="广西"></el-option>
            <el-option label="内蒙古" value="内蒙古"></el-option>
            <el-option label="宁夏" value="宁夏"></el-option>
            <el-option label="西藏" value="西藏"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" style="width: 500px">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="景点编号" style="width: 500px">
          <el-input v-model="form.areaId" disabled></el-input>
        </el-form-item>
        <el-form-item label="景点图片" style="width: 500px">
          <img :src="form.pic" alt="图片加载失败" style="width:380px;height:220px">
        </el-form-item>
        <el-form-item label="更换景点图片">
          <el-upload
            action=""
            list-type="picture-card"
            :file-list="fileList"
            :http-request="upload"
            :before-upload="beforeUpload"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="景点介绍">
          <el-input
            type="textarea"
            :rows="8"
            v-model="form.introduction"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import utils from '@/assets/utils'

export default {
  data() {
    return {
      token: window.localStorage.getItem("token"),
      page: 1,
      limit: 10,
      totalCount: 0,
      tableList: [],
      dialogFormVisible: false,
      form: {},
      fileList: [],
      imgForm: new FormData(),
      imgUrl: "",
      fileName: "",
      fileUrl: "",
      seachform: {},
    };
  },
  mounted() {
    this.fn();
    utils.$on('getMyJdList', () => {
      this.fn();
    })
  },
  methods: {
    fn() {
      this.$axios({
        method: "post",
        url: "/scenicarea/list/current",
        headers: { token: this.token },
        data: {
          page: this.page,
          limit: this.limit,
        },
      }).then((res) => {
        if (res.data.code === 0) {
          this.totalCount = res.data.page.totalCount;
          this.tableList = res.data.page.regard;
          this.tableList.forEach((item) => {
            item.status =
              item.status === 1
                ? "审核通过"
                : item.status === 0
                ? "未审核"
                : "未通过或撤销";
          });
          // console.log(this.tableList);
        }
        if (res.data.code === 500) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
    handleSizeChange(val) {
      this.limit = val;
      this.fn();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.fn();
    },
    rowStyle() {
      return { height: "60px" };
    },
    cellStyle(k) {
      if (k.columnIndex === 3) {
        if (k.row.status === "审核通过") {
          return { color: "#33CC00" };
        } else if (k.row.status === "未审核") {
          return { color: "blue" };
        } else {
          return { color: "red" };
        }
      }
    },
    edit(k) {
      this.form = k;
      this.dialogFormVisible = true;
    },
    clear(k) {
      let id = k.areaId;
      this.$axios({
        method: "post",
        url: "/scenicarea/del",
        headers: { token: this.token },
        data: { areaId: id },
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          this.$message({
            type: "success",
            message: "删除成功！",
          });
          this.fn();
        }
      });
    },
    beforeUpload(file) {
      if (file) {
        this.imgForm.append("file", file);
      } else {
        return false;
      }
    },
    upload() {
      this.$axios({
        method: "post",
        url: "/oss/upload/scenic",
        headers: { token: this.token },
        data: this.imgForm,
      }).then((res) => {
        let data = res.data;
        if (data.code !== 0) {
          this.$message.error(res.data.msg);
          return false;
        }
        this.fileName = data.name;
        this.fileUrl = data.url;
        this.form.pic = this.fileUrl;
        // this.$message.success("上传成功");
      });
    },
    submit() {
      this.dialogFormVisible = false;
      let data = {};
      for (let i in this.form) {
        data["name"] = this.form["name"];
        data["address"] = this.form["address"];
        data["introduction"] = this.form["introduction"];
        data["pic"] = this.form["pic"];
        data["areaId"] = this.form["areaId"];
        if (this.form["parentAreaName"]) {
          data["parentAreaName"] = this.form["parentAreaName"];
        }
      }
      this.$axios({
        method: "post",
        url: "/scenicarea/edit",
        headers: { token: this.token },
        data: data,
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          this.fn();
        }
      });
    },
    search() {
      this.seachform.page = this.page;
      this.seachform.limit = this.limit;
      let data = this.seachform;
      // console.log(data);
      this.$axios({
        method: "post",
        url: "/scenicarea/list/current",
        headers: { token: this.token },
        data: data,
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          // this.totalCount = res.data.page.totalCount;
          if (res.data.page) {
            this.tableList = res.data.page.regard;
            this.tableList.forEach((item) => {
              item.userId =
                item.userId === 1
                  ? "管理员"
                  : item.userId === 2
                  ? "商家"
                  : "旅客";
              item.status =
                item.status === 1
                  ? "审核通过"
                  : item.status === 0
                  ? "未审核"
                  : "未通过或撤销";
            });
          } else {
            this.tableList = [];
          }
        }
        if (res.data.code === 500) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
  },
};
</script>
<style  scoped>
.seachform {
  margin: 10px 0 0 0;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.searchbtn {
  width: 80px;
  height: 40px;
  margin: 0 15px;
}
</style>>
